/**
 * @Title: UserController
 * @Package cn.com.yiqi.wsc.portal.controller.user
 * @Description: TODO
 * Copyright: Copyright (c) 2016
 * Company: 成都壹柒互动科技有限公司
 * @author 会员系统组-胥钰淋
 * @date 2017/2/13 18:57
 * @version V1.0
 */
package cn.com.yiqi.wsc.portal.controller.user;

import cn.com.artemis.framework.service.message.service.yunxin.impl.YunxinSMSServiceImpl;
import cn.com.yiqi.wsc.Ro.OrgVO;
import cn.com.yiqi.wsc.common.Result;
import cn.com.yiqi.wsc.common.ResultStatusEnum;
import cn.com.yiqi.wsc.common.ResultUtil;
import cn.com.yiqi.wsc.dao.user.criteria.UserCriteria;
import cn.com.yiqi.wsc.entity.org.OrgEntity;
import cn.com.yiqi.wsc.entity.org.OrgExtendEntity;
import cn.com.yiqi.wsc.entity.org.OrgLinkEntity;
import cn.com.yiqi.wsc.enumeration.CommonEnum;
import cn.com.yiqi.wsc.enumeration.UserType;
import cn.com.yiqi.wsc.portal.controller.BaseController;
import cn.com.yiqi.wsc.service.org.OrgService;
import cn.com.yiqi.wsc.service.user.UserService;
import cn.com.yiqi.wsc.service.user.dataobject.UserDO;
import cn.com.yiqi.wsc.service.user.dataobject.UserForModifyPassword;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author 会员系统组-胥钰淋
 * @ClassName: UserController
 * @Description: TODO
 * @date 2017/2/13 18:57
 */
@Controller
@RequestMapping("/user")
public class UserController extends BaseController {
    @Autowired
    private OrgService orgService;

    @Autowired
    private YunxinSMSServiceImpl YunxinSMSServiceImpl;

    @Value("#{APP_PROP['user.initPassword']}")
    private String initPassword;

    @Value("#{APP_PROP['yunxin.sms.resetPassword.templateId']}")
    private Integer templateId;


    @RequestMapping("/index")
    public ModelAndView index() {
        ModelAndView mv = new ModelAndView("user/index");
        mv.addObject("userList", "[]");
        mv.addObject("totalCount", 0);
        mv.addObject("pageSize", 10);
        this.getUserData(mv);
        return mv;
    }

    /**
     * 保存
     *
     * @param userDO
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Result save(@RequestBody UserDO userDO) {
        Result result = new Result();

        if (userDO.getPassword() == null) {
            userDO.setPassword(initPassword);
        }

        if (userService.isExist(userDO.getTel())) {
            return ResultUtil.paramError(result, "手机号已存在");
        }

        Integer userId = userService.getCurrentUserId();

        userDO.setId(userId);
        userDO.setLoginName(userDO.getTel());
        //默认用户类型
        userDO.setType(UserType.user.getValue());
        //默认启用
        userDO.setEnabled(CommonEnum.ENABLE.getValue());
        result = userService.insertUser(userDO);

        return result;
    }

    /**
     * 分页查询
     *
     * @param criteria
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public Result list(@RequestBody UserCriteria criteria) {
        Result result = new Result();

        result = userService.pageList(criteria);
        result.setStatus(200);

        return result;
    }

    /**
     * 修改用户信息
     *
     * @param userDO
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Result update(@RequestBody UserDO userDO) {

        Result result = new Result();


        Integer userId = userService.getCurrentUserId();
        userDO.setUpdateUserId(userId);

        result = userService.updateUser(userDO);

        return result;
    }


    /**
     * 获取详情
     *
     * @param userDO
     * @return
     */
    @RequestMapping(value = "/detail", method = RequestMethod.POST)
    @ResponseBody
    public Result detail(@RequestBody UserDO userDO) {

        Result result = new Result();

        result = userService.detail(userDO);

        return result;
    }

    /**
     * 获取详情
     *
     * @param userDO
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@RequestBody UserDO userDO) {

        Result result = new Result();

        UserDO userDOForDelete = new UserDO();
        userDOForDelete.setId(userDO.getId());
        userDOForDelete.setDelete(CommonEnum.DELETED.getValue());

        result = userService.updateUser(userDOForDelete);

        return result;
    }


    /**
     * 获取详情
     *
     * @param userDO
     * @return
     */
    @RequestMapping(value = "/enable", method = RequestMethod.POST)
    @ResponseBody
    public Result enable(@RequestBody UserDO userDO) {

        Result result = new Result();

        if (userDO.getId() == null) {
            result.setStatus(ResultStatusEnum.参数错误.getCode());
            result.setErrMsg("参数错误");
            return result;
        }


        result = userService.enable(userDO);

        return result;
    }

    /**
     * 获取详情
     *
     * @param userDO
     * @return
     */
    @RequestMapping(value = "/disable", method = RequestMethod.POST)
    @ResponseBody
    public Result disable(@RequestBody UserDO userDO) {

        Result result = new Result();

        if (userDO.getId() == null) {
            result.setStatus(ResultStatusEnum.参数错误.getCode());
            result.setErrMsg("参数错误");
            return result;
        }

        result = userService.disable(userDO);

        return result;
    }

    /**
     * 修改密码
     *
     * @param user
     * @return
     */
    @RequestMapping(value = "/modifyPassword", method = RequestMethod.POST)
    @ResponseBody
    public Result modifyPassword(@RequestBody UserForModifyPassword user) {

        Result result = new Result();
        if (!StringUtils.isEmpty(user.getPassword()) && !StringUtils.isEmpty(user.getNewPassword())) {
            user.setId(userService.getCurrentUserId());
            Integer i = userService.modifyPassword(user);
            if (i == 1) {
                result.setStatus(ResultStatusEnum.操作成功.getCode());
                result.setErrMsg("密码修改成功");
                Subject subject = SecurityUtils.getSubject();
                subject.logout();
                return result;
            } else {
                result.setStatus(ResultStatusEnum.参数错误.getCode());
                result.setErrMsg("旧密码错误");
                return result;
            }

        } else {
            result.setStatus(ResultStatusEnum.参数错误.getCode());
            result.setErrMsg("密码不能为空");
            return result;
        }

    }

    /**
     * 获取详情
     *
     * @param userDO
     * @return
     */
    @RequestMapping(value = "/getMyInfo", method = RequestMethod.POST)
    @ResponseBody
    public Result getMyInfo(@RequestBody UserDO userDO) {
        userDO.setId(userService.getCurrentUserId());
        return userService.getUserDetail(userDO);
    }

    /**
     * 获取我的商家详情
     *
     * @param adminOrg
     * @return
     */
    @RequestMapping(value = "/getMyOrg", method = RequestMethod.POST)
    @ResponseBody
    public Result myOrgInfo(@RequestBody OrgEntity adminOrg) {
        Result result = new Result();
        if (StringUtils.isEmpty(adminOrg.getLinkTel())) {
            return ResultUtil.paramError(result, "手机号码不能为空");
        }
        adminOrg.setId(userService.getCurrentUser().getOrgId());
        return orgService.selectOrgById(adminOrg);
    }

    /**
     * 修改我的商家信息
     *
     * @param orgVO
     * @return
     */
    @RequestMapping(value = "/updateMyOrg", method = RequestMethod.POST)
    @ResponseBody
    public Result updateMyOrg(@RequestBody OrgVO orgVO) {


        orgVO.setId(userService.getCurrentUser().getOrgId());
        OrgEntity orgEntity = new OrgEntity();
        orgEntity.setId(orgVO.getId());
        orgEntity.setPid(orgVO.getPid());
        orgEntity.setName(orgVO.getName());
        orgEntity.setCity(orgVO.getCityCode());
        orgEntity.setAddress(orgVO.getAddress());
        orgEntity.setZipCode(orgVO.getZipCode());
        orgEntity.setFax(orgVO.getFax());
        orgEntity.setEmail(orgVO.getEmail());
        orgEntity.setLinkName(orgVO.getLinkName());
        orgEntity.setLinkTel(orgVO.getLinkTel());
        orgEntity.setCharacter(orgVO.getCharacter());//性质

        OrgExtendEntity orgExtendEntity = new OrgExtendEntity();
        orgExtendEntity.setOrgId(orgEntity.getId());
        orgExtendEntity.setLicenseCode(orgVO.getLicenseCode());//营业执照编码
        orgExtendEntity.setBusinessScope(orgVO.getBusinessScope());
        orgExtendEntity.setLicensePicUrl(orgVO.getLicensePicUrl());//营业执照图片
        orgExtendEntity.setStaffNumber(orgVO.getStaffNumber());//员工数量

        OrgLinkEntity orgLinkEntity = new OrgLinkEntity();
        orgLinkEntity.setOrgId(orgEntity.getId());
        orgLinkEntity.setType(1);
        orgLinkEntity.setName(orgVO.getLegalPersonName());

        return orgService.updateOrg(orgEntity, orgExtendEntity, orgLinkEntity, userService.getCurrentUserId());

    }

    /**
     * 重置密码
     *
     * @param user
     * @return
     */
    @RequestMapping(value = "/resettingPwd", method = RequestMethod.POST)
    @ResponseBody
    public Result resttingPassword(@RequestBody UserForModifyPassword user) {

        Result result = userService.resttingPassword(user);

        if (result.getData().get("rtp").equals(1)) {
            //成功 短信
            if (templateId != null) {
                YunxinSMSServiceImpl.sendTemplate(templateId, "[" + user.getTel()+ "]", "[" + initPassword + "]");
            }
        }

        return result;
    }
}
